﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace AutoPunchApp
{
    public static class Helper
    {
        private static PunchConfig config;
        private static string filename = "";

        static Helper()
        {
            filename = AppDomain.CurrentDomain.BaseDirectory + "config.xml";
            ReloadConfig();
        }

        public static PunchConfig Config
        {
            get { return config; }
        }

        public static void SaveConfig()
        {
            FileStream stream = null;
            XmlSerializer serializer = new XmlSerializer(typeof(PunchConfig));
            try
            {
                stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
                serializer.Serialize(stream, config);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        public static void ReloadConfig()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PunchConfig));
            FileStream stream = null;
            try
            {
                stream = new FileStream(filename, FileMode.Open);
                config = (PunchConfig)serializer.Deserialize(stream);
            }
            catch (Exception ex)
            {
                config = new PunchConfig();
                //throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        public static bool IsValid(this DateTime date)
        {
            return date.CompareTo(DateTime.MinValue) > 0 && date.CompareTo(DateTime.MaxValue) < 0;
        }
    }

    public class PunchConfig
    {
        public long OnDutyTime { get; set; }
        public long OffDutyTime { get; set; }
        public Action AfterOffDutyActon { get; set; }
        public DateType PunchDateType { get; set; }
        public DateTime OnceDate { get; set; }
        public string PunchWeekDays { get; set; }
        public string PunchMonthDays { get; set; }
    }

    public enum Action
    {
        Suspend,
        Shutdown,
        Nothing
    }

    public enum DateType
    {
        Once,
        Week,
        Days
    }

    public enum PunchResult
    {
        Success,
        Repeat,
        Fail
    }
}
